package cn.wuxq.permission;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class IPermission {

    public final static String[] CAMERA = new String[]{"android.permission.CAMERA"};
    public final static String[] EXTERNAL_STORAGE = new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"};

    private Activity activity;
    private Fragment fragment;

    private int requestCode;
    private OnPermissionListener mListener;

    public IPermission(Activity activity) {
        this.activity = activity;
    }

    public IPermission(Fragment fragment) {
        this.fragment = fragment;
    }

    public IPermission(Activity activity, OnPermissionListener listener) {
        this.activity = activity;
        this.mListener = listener;
    }

    public IPermission(Fragment fragment, OnPermissionListener listener) {
        this.fragment = fragment;
        this.mListener = listener;
    }

    public boolean requestPermission(String[] permissions, int requestCode) {
        this.requestCode = requestCode;
        for (String permission : permissions) {
            if (activity != null && !activity.isDestroyed()) {
                if (ContextCompat.checkSelfPermission(activity, permission) != 0) {
                    ActivityCompat.requestPermissions(activity, permissions, requestCode);
                    return false;
                }
            } else if (fragment != null && fragment.getActivity() != null && !fragment.getActivity().isDestroyed()) {
                if (ContextCompat.checkSelfPermission(fragment.getActivity(), permission) != 0) {
                    fragment.requestPermissions(permissions, requestCode);
                    return false;
                }
            }
        }
        return true;
    }

    public void loadPermissionsResult(int request, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (mListener == null) {
            return;
        }
        if (requestCode == request) {
            List<String> passPermissions = new ArrayList<>();
            List<String> rejectPermissions = new ArrayList<>();
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == 0) {
                    passPermissions.add(permissions[i]);
                } else {
                    rejectPermissions.add(permissions[i]);
                }
            }
            mListener.onRequestPermissionResult(rejectPermissions.size() == 0, passPermissions, rejectPermissions, requestCode);
        }
    }

    public void setOnPermissionListener(OnPermissionListener listener) {
        this.mListener = listener;
    }

    /**
     * 其他
     */
    public void goIntentSetting(@NonNull Context context) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
        intent.setData(uri);
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
